import type { FileControllerApi } from '../openapi';
import type { ChunkProgressHandler } from './types';

import { FileUpload } from './file-upload';

/**
 * 分享集合类 - 管理多个文件的上传和分享
 */
export class ShareCollection {
  private readonly files: string[] = [];
  private readonly fileUpload: FileUpload;

  constructor(fileApi: FileControllerApi, chunkSize: number = 1024 * 1024) {
    this.fileUpload = new FileUpload(fileApi, chunkSize);
  }

  /**
   * 添加文件到分享集合
   */
  async addFile(file: File, onProgress?: ChunkProgressHandler): Promise<void> {
    const fileMd5 = await this.fileUpload.uploadFile(file, {
      onProgress: (progressInfo) => {
        console.log(`上传进度：`, progressInfo);
        onProgress?.(progressInfo);
      },
    });

    this.files.push(fileMd5);
  }

  /**
   * 清空分享集合
   */
  clear(): void {
    this.files.length = 0;
  }

  /**
   * 获取文件数量
   */
  getFileCount(): number {
    return this.files.length;
  }

  /**
   * 获取已添加的文件MD5列表
   */
  getFiles(): string[] {
    return [...this.files];
  }
}
